package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuImageService;
import com.atguigu.gmall.product.service.SpuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:hrh
 * @create: 2022-11-03 22:58
 * @Description: Spu管理
 */
@RestController
@RequestMapping ("/admin/product")
@Api ("Spu管理")
public class SpuController {

    @Autowired
    SpuImageService spuImageService;

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;


    /**
     * 查询
     *
     * @param page:页码
     * @param limit:每页行数
     * @param category3Id:三级分类Id
     * @return ：Page->查询的Spu
     */
    @GetMapping ("/{page}/{limit}")
    @ApiOperation ("分页查询")
    public Result getList (@ApiParam ("页码") @PathVariable Long page,
                           @ApiParam ("页行数") @PathVariable Long limit,
                           @ApiParam ("对应的三级分类Id") @RequestParam Long category3Id) {


        QueryWrapper<SpuInfo> wrapper = new QueryWrapper<> ();
        Page<SpuInfo> spuInfoPage = new Page<> (page, limit);
        wrapper.eq ("category3_id", category3Id);
        Page<SpuInfo> pages = this.spuInfoService.page (spuInfoPage, wrapper);

        return Result.ok (pages);
    }

    /**
     * 添加Spu数据
     *
     * @param spuInfo
     * @return
     */
    @PostMapping ("/saveSpuInfo")
    public Result saveSpuInfo (@RequestBody SpuInfo spuInfo) {
        this.spuInfoService.saveSpuInfo (spuInfo);
        return Result.ok ();
    }

    /**
     * 通过Spu查询Sku销售属性列表
     *
     * @param spuId
     * @return SpuSaleAttr
     */
    @GetMapping ("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList (@PathVariable Long spuId) {
        List<SpuSaleAttr> spuSaleAttrs = this.spuSaleAttrService.getListBySpuId (spuId);
        return Result.ok (spuSaleAttrs);
    }

    /**
     * 通过SpuId 查询Image图片
     *
     * @param spuId
     * @return：List<SpuImage>
     */
    @GetMapping ("/spuImageList/{spuId}")
    public Result spuImageListBySpuId (@PathVariable Long spuId) {
        List<SpuImage> spuImages = this.spuImageService.getListBySpuId (spuId);
        return Result.ok (spuImages);
    }

    @PostMapping ("/saveSkuInfo")
    public Result saveSkuInfo (@RequestBody SkuInfo skuInfo) {
        if (skuInfo == null) {
            return Result.ok ().message ("请检查是否有传入数据");
        }
        this.skuInfoService.saveSkuInfo (skuInfo);
        return Result.ok ();
    }


}
